/*
This file is part of Docking@Home.
http://docking.gcl.cis.udel.edu
Copyright (C) 2009 University of Delaware

Docking@Home is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Docking@Home is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Docking@Home.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifdef _WIN32
#include "boinc_win.h"
#include "windows.h"
#else
#include <math.h>
#endif

#ifdef _LINUX
#include <sys/resource.h>
#endif

#include "parse.h"
#include "util.h"
#include "gutil.h"
#include "boinc_gl.h"
#include "app_ipc.h"
#include "boinc_api.h"
#include "graphics2.h"
#include "txf_util.h"
#include "str_util.h"
#include "diagnostics.h"
#include "filesys.h"
#include "mfile.h"

#include "GLHelper.h"
#include "Logo.h"
#include "./Tabs/TabbedGUI.h"

#ifdef __APPLE__
#include "mac/app_icon.h"
#endif

using std::string;

bool run_slow = false;
bool early_exit = false;
bool early_crash = false;
bool early_sleep = false;
double cpu_time = 2000;

// Global Variables - Not good but here for now
APP_INIT_DATA uc_aid;
TEXTURE_DESC logo;
TabbedGUI * tabbedGUI;
bool bFullScreen = false;

static void parse_project_prefs(char* buf) 
{
	char cs[256];
	COLOR c;
	double hue;
	if (!buf) return;
	if (parse_str(buf, "<color_scheme>", cs, 256)) {
		if (!strcmp(cs, "Tahiti Sunset")) {
			hue = .9;
		} else if (!strcmp(cs, "Desert Sands")) {
			hue = .1;
		} else {
			hue = .5;
		}
		HLStoRGB(hue, .5, .5, c);
	}
}

void app_graphics_init() 
{
	boinc_parse_init_data_file();
	boinc_get_init_data(uc_aid);
	parse_project_prefs(uc_aid.project_preferences);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	tabbedGUI = new TabbedGUI();
	tabbedGUI->init(uc_aid);
}

void app_graphics_render(int xs, int ys, double time_of_day) 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Render the Windowed GUI
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	tabbedGUI->draw(xs, ys, bFullScreen);
}

void app_graphics_resize(int w, int h)
{
	int viewport[4];

	get_viewport(viewport);
	glViewport(0, 0, w, h);

	tabbedGUI->resize(w, h, bFullScreen);
}

void app_graphics_reread_prefs()
{
	boinc_parse_init_data_file();
	boinc_get_init_data(uc_aid);
	parse_project_prefs(uc_aid.project_preferences);
}

void app_graphics_close ()
{
	if (tabbedGUI != NULL)
	{
		delete tabbedGUI;
		tabbedGUI = NULL;
	}
}

void boinc_app_mouse_move(int x, int y, int left, int middle, int right) 
{
	tabbedGUI->handleMouseMove(x, y, left, middle, right);
}

void boinc_app_mouse_button(int x, int y, int which, int is_down) 
{
	tabbedGUI->handleMouseButton(x, y, which, is_down);
}

void boinc_app_key_press(int key1, int key2)
{
	tabbedGUI->handleKeyPress(key1, key2);
}

void boinc_app_key_release(int key1, int key2)
{
	tabbedGUI->handleKeyRelease(key1, key2);
}

int main(int argc, char** argv) {

	int i;
	int retval = 0;

	for (i=0; i<argc; i++) {
		if (!strcmp(argv[i], "-early_exit")) early_exit = true;
		if (!strcmp(argv[i], "-early_crash")) early_crash = true;
		if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
		if (!strcmp(argv[i], "-run_slow")) run_slow = true;
		if (!strcmp(argv[i], "-cpu_time"))	cpu_time = atof(argv[++i]);
		if (!strcmp(argv[i], "--fullscreen"))	bFullScreen = true;
	}

	boinc_init_graphics_diagnostics(BOINC_DIAG_DEFAULTS);

#ifdef __APPLE__
	setMacIcon(argv[0], MacAppIconData, sizeof(MacAppIconData));
#endif

	boinc_parse_init_data_file();
	boinc_get_init_data(uc_aid);
	if (uc_aid.project_preferences) {
		parse_project_prefs(uc_aid.project_preferences);
	}
	boinc_graphics_loop(argc, argv);

	app_graphics_close();

	boinc_finish_diag();
}
